package com.taptech.doufu.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.BaseListAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.adapter.personalcenter.PersonalCardInfoAapater;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-20
 */
public class PersonalAttentionsActivity extends DiaobaoBaseActivity implements HttpResponseListener
{

    private PullToRefreshListView attentions;
    private BaseListAdapter adapter;
    private TextView typeName;
    private String userId;

    private LinearLayout tipLayout;
    private String last = "";

    private boolean loadComplete = true;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        userId = getIntent().getStringExtra(Constant.UID);
        setContentView(R.layout.personal_center_activity_fans);
        initView();

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus)
        {
            if (adapter.getCount() == 0)
            {
                loadDate();
            }
        }

    }

    private void loadDate()
    {
        if (loadComplete)
        {
            loadComplete = false;
            last = "";
            if (userId == null || userId.equals(""))
            {
                PersonalInfoService.getInstance().loadAttentions(last, this);
            } else
            {
                PersonalInfoService.getInstance().loadOtherAttentions(userId, last, this);
            }

        }
    }

    private void loadMoreDate()
    {
        if (loadComplete)
        {
            loadComplete = false;
            if (userId == null || userId.equals(""))
            {
                PersonalInfoService.getInstance().loadAttentions(last, this);
            } else
            {
                PersonalInfoService.getInstance().loadOtherAttentions(userId, last, this);
            }

        }
    }

    private void initView()
    {
        typeName = (TextView) findViewById(R.id.member_type_name);
        attentions = (PullToRefreshListView) findViewById(R.id.personal_center_fans_list);
        attentions.setLoadmoreable(true);
        attentions.setRefreshable(true);
        tipLayout = (LinearLayout) findViewById(R.id.personal_center_fans_tip);
        attentions.setOnLoadAndRefreshListener(new PullToRefreshListView.OnLoadAndRefreshListener()
        {

            @Override
            public void onRefresh()
            {
                loadDate();
            }

            @Override
            public void loadMore()
            {
                // TODO Auto-generated method stub

            }
        });
        attentions.setOnScrollToBottomListener(new PullToRefreshListView.OnScrollToBottomListener()
        {

            @Override
            public void OnScrollToBottom()
            {
                loadMoreDate();
            }
        });
        adapter = new PersonalCardInfoAapater(this);
        ((PersonalCardInfoAapater) adapter).setShowType(0);
        if (userId == null || userId.equals(""))
        {
            typeName.setText("关注");
        } else
        {
            if (AccountService.getInstance().isUserSelf(userId))
            {
                typeName.setText("我关注的");
            } else
            {
                typeName.setText("TA关注的");
            }

        }
        attentions.setAdapter(adapter);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        try
        {
            loadComplete = true;
            if (response.getStatus() == 0)
            {
                JSONArray datas = (JSONArray) response.getData();
                JSONObject meta = new JSONObject(response.getMeta().toString());
                List<PersonalCardInfo> contacts = DiaobaoUtil.jsonArray2BeanList(PersonalCardInfo.class, datas);
                if (last.equals(""))
                {
                    adapter.setDataSource(contacts);
                    attentions.setFull(false);
                    attentions.onRefreshComplete();
                    attentions.loadMoreComplete();
                    attentions.setLoadmoreable(true);

                } else if (!meta.isNull("last") && !last.equals(meta.getString("last")))
                {
                    adapter.setSuperDatas(contacts);
                    attentions.setFull(false);
                    attentions.loadMoreComplete();
                    attentions.setLoadmoreable(true);
                } else

                {
                    attentions.setFull(true);
                    attentions.loadMoreComplete();
                    attentions.setLoadmoreable(false);
                }
                if (!meta.isNull("last"))
                {
                    last = meta.getString("last");
                }

                if (userId == null || userId.equals(""))
                    PersonalInfoService.getInstance().setAttentions(adapter.getDataSource());
            } else
            {
                UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
        if (adapter.getCount() == 0)
        {
            tipLayout.setVisibility(View.VISIBLE);
        } else
        {
            tipLayout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (PersonalInfoService.getInstance().isAttentionsChanged() && (userId == null || userId.equals("")))
        {
            adapter.setDataSource(PersonalInfoService.getInstance().getAttentions());
        }
    }

    public void back(View view)
    {
        onBackPressed();
    }

    @Override
    public void onBackPressed()
    {
        PersonalInfoService.getInstance().refreshMyAttentions();
        super.onBackPressed();
    }

}
